using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;

public class LogicalEffectManager : MonoBehaviour
{
    [HideInInspector]
    public int initEffectNameList = 0;
    [HideInInspector]
    public int initBanedEffectNameList = 0;
    public List<LogicalEffectBase> effectList = new List<LogicalEffectBase>();
    private List<string> banedEffectNameList = new List<string>();
    public EffectSystem effectSystem;
    private bool canUpdate = false;
    private void Awake()
    {
        effectList = new List<LogicalEffectBase>();
        effectSystem = EffectSystem.Instance;
        foreach (var item in effectSystem.GetEffectNamesByIndex(initBanedEffectNameList))
        {
            banedEffectNameList.Add(item);
        }
    }
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (!canUpdate) return;
        for (int i = 0; i < effectList.Count; i++)
        {
            LogicalEffectBase effect = effectList[i];
            effect.OnUpdate();
            if (!effect.GetLifeState())
            {
                effect.OnComplete();
                if (effectList.Count == 0) return;
                effectList.RemoveAt(i);
            }
        }
    }
    private void OnDestroy()
    {
        ClearAllEffect();
    }
    public List<LogicalEffectBase> GetEffectByName(string effectName)
    {
        return effectList.Where(effect => effect.effectData.effectName == effectName).ToList();
    }
    /// <summary>
    /// 添加Effect
    /// </summary>
    /// <param name="effect"></param>
    public void OnAddEffect(LogicalEffectBase effect)
    {
        if (banedEffectNameList.Contains(effect.effectData.effectName)) return;
        effect.OnApply();
        if (!effect.GetLifeState())
        {
            effect.OnComplete();
        }
        else
        {
            effectList.Add(effect);
            canUpdate = true;
        }
    }
    /// <summary>
    /// 删除Effect
    /// </summary>
    /// <param name="effect"></param>
    public void OnRemoveEffect(LogicalEffectBase effect)
    {
        int index = effectList.IndexOf(effect);
        if (index != -1)
        {
            effectList[index].OnRemove();
            effectList.RemoveAt(index);
            RefreshEffect();
        }
        if(effectList.Count == 0)
        {
            canUpdate = false;
        }
    }
    /// <summary>
    /// 刷新Effect
    /// </summary>
    public void RefreshEffect()
    {
        if (effectList.Count > 0)
        {
            foreach (var item in effectList)
            {
                item.OnRefresh();
            }
        }
    }
    /// <summary>
    /// 清除所有Effect
    /// </summary>
    public void ClearAllEffect()
    {
        foreach (var item in effectList)
        {
            item.OnRemove();
        }
        effectList.Clear();
        canUpdate = false;
    }
    public void AddBanedEffect(string effectName)
    {
        banedEffectNameList.Add(effectName);
        for (int i = 0; i < effectList.Count; i++)
        {
            if (effectList[i].effectData.effectName == effectName)
            {
                effectList[i].OnRemove();
                effectList.RemoveAt(i);
            }
        }
        if (effectList.Count == 0)
        {
            canUpdate = false;
        }
    }
    public void RemeveBanedEffect(string effectName)
    {
        banedEffectNameList.Remove(effectName);
    }
    public void ClearAllBanedEffect()
    {
        banedEffectNameList.Clear();
    }
    /// <summary>
    /// 销毁EffectManager 并清除所有Effect
    /// </summary>
    public void DestroyEffect()
    {
        canUpdate = false;
        ClearAllEffect();
        ClearAllBanedEffect();
        // TODO:判断是否需要对象池回收
        BasePoolObject poolObject = GetComponent<BasePoolObject>();
        if (poolObject != null)
        {
            poolObject.OnDestroyToBack();
        }
        else
        {
            Destroy(gameObject);
        }

    }
}
